package cn.supermaster.management.core.component;

import cn.supermaster.management.common.constant.MessageConstant;
import cn.supermaster.management.common.utils.*;
import cn.supermaster.management.core.manager.RecordLogManager;
import cn.supermaster.management.system.service.ISystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import cn.supermaster.management.common.constant.Constants;
import cn.supermaster.management.common.constant.ShiroConstant;
import cn.supermaster.management.common.constant.UserConstant;
import cn.supermaster.management.common.domain.SystemUser;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.core.service
 * @ClassName: RegisterManager
 * @Description: [系统注册管理器]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/11 0:38
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Component
public class RegisterManager {
    @Autowired
    private ISystemUserService userService;

    @Autowired
    private PasswordManager passwordManager;

    /**
     * @Description: [用户注册]
     * <p></P>
     * @Title: doAccountRegister
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: user 用户信息
     * @Return: java.lang.String
     */
    public String doAccountRegister(SystemUser user) {
        String message = "";
        String username = user.getUserName();
        String password = user.getPassword();
        String salt = user.getSalt();
        // 设置用户名和密码校验信息
        doCheckRegisterMessage(username, password);
        //
        user.setPwdUpdateDate(DateUtils.getNowDate());
        user.setUserName(username);
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(passwordManager.encryptPassword(username, password, salt));
        boolean register = userService.registerUser(user);
        if (!register) {
            message = "注册失败,请联系系统管理人员";
        } else {
            RecordLogManager.doRecordLoginInformation(username, Constants.REGISTER, MessageConstant.USER_REGISTER_SUCCESS, null);
        }

        return message;
    }

    /**
     * @Description: [设置用户名和密码校验信息]
     * <p></P>
     * @Title: doCheckRegisterMessage
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: username 用户名
     * @Param: password 密码
     * @Return: java.lang.String
     * @Throws:
     */
    private String doCheckRegisterMessage(String username, String password) {
        String message = "";

        Object captcha = ServletUtils.getRequest().getAttribute(ShiroConstant.CURRENT_CAPTCHA);// 获取当前请求对象验证码
        int passwordMinSize = UserConstant.PASSWORD_MIN_LENGTH;//密码范围最小阈值
        int passwordMaxSize = UserConstant.PASSWORD_MIN_LENGTH;//密码范围最大阈值
        int usernameMinSize = UserConstant.USERNAME_MIN_LENGTH;//用户名范围最小值
        int usernameMaxSize = UserConstant.USERNAME_MAX_LENGTH;//用户名范围最大值

        // 验证码校验
        if (!StringUtils.isEmpty(captcha)) message = "验证码错误";

        // 用户名不能为空
        if (CheckUtils.isEmpty(username)) message = "用户名不能为空";

        // 密码不能为空
        if (CheckUtils.isEmpty(password)) message = "用户密码不能为空";

        // 密码长度规则校验
        if (!CheckUtils.checkDigitalScope(passwordMinSize, password.length(), passwordMaxSize))
            message = "密码长度必须在5到20个字符之间";

        // 用户名长度规则校验
        if (!CheckUtils.checkDigitalScope(usernameMinSize, username.length(), usernameMaxSize))
            message = "账户长度必须在2到20个字符之间";

        // 用户已经存在
        String existUserStatus = userService.checkuserNameUnique(username);
        if (UserConstant.USER_NAME_NOT_UNIQUE.equals(existUserStatus))
            message = "保存用户'" + username + "'失败，注册账号已存在";

        // 返回注册信息
        return message;
    }
}
